//
//  USTextView.m
//  usale0001
//
//  Created by wen on 16/12/5.
//  Copyright © 2016年 niusaibing. All rights reserved.
//

#import "BDTextView.h"
#import "LiuqsDecoder.h"

@interface BDTextView ()<UITextViewDelegate>
@property (nonatomic, strong) YYLabel *placeHolderLabel;
@end

@implementation BDTextView

#pragma mark Initialisation


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.delegate = self;
        self.returnKeyType = UIReturnKeyDone;
    }
    return self;
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.placeHolderLabel.text = placeholder;
    self.placeHolderLabel.numberOfLines = 0;
    self.placeHolderLabel.width = kBDScreenWidth - 36;
    self.placeHolderLabel.height = [placeholder heightWithFont:self.placeHolderLabel.font width:self.placeHolderLabel.width];
    self.placeHolderLabel.top = 8;
    self.placeHolderLabel.left = 8;
}

- (void)setText:(NSString *)text{
    [super setText:text];
    if ([text isNotEmpty]) {
        self.placeHolderLabel.hidden = YES;
    }else{
        self.placeHolderLabel.hidden = NO;
    }
}

- (void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    if (attributedText.length > 0) {
        self.placeHolderLabel.hidden = YES;
    }else {
        self.placeHolderLabel.hidden = NO;
    }
}

- (void)setPlaceHolderFont:(UIFont *)placeHolderFont{
    _placeHolderFont = placeHolderFont;
    self.placeHolderLabel.font = placeHolderFont;
    self.font = placeHolderFont;
    self.placeHolderLabel.width = kBDScreenWidth - 36;
    self.placeHolderLabel.height = [self.placeHolderLabel.text heightWithFont:placeHolderFont width:self.placeHolderLabel.width];
    self.placeHolderLabel.top = 8;
    self.placeHolderLabel.left = 8;
}

#pragma mark Dealloc

- (YYLabel *)placeHolderLabel{
    if (!_placeHolderLabel) {
        self.placeHolderLabel = [[YYLabel alloc] init];
        [self addSubview:_placeHolderLabel];
        _placeHolderLabel.textColor = [UIColor lightGrayColor];
        _placeHolderLabel.text = self.placeholder;
        _placeHolderLabel.userInteractionEnabled = YES;
        [_placeHolderLabel sizeToFit];
        _placeHolderLabel.top = 0;
        _placeHolderLabel.left = 8;
    }
    return _placeHolderLabel;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextViewShouldBeginEditing:)]) {
        return [self.textViewDelegate bdTextViewShouldBeginEditing:self];
    }
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextViewShouldEndEditing:)]) {
        return [self.textViewDelegate bdTextViewShouldEndEditing:self];
    }
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.placeHolderLabel.hidden = YES;
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextViewDidBeginEditing:)]) {
        [self.textViewDelegate bdTextViewDidBeginEditing:self];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if (self.text.length == 0) {
        self.placeHolderLabel.hidden = NO;
    }
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextViewDidEndEditing:)]) {
        [self.textViewDelegate bdTextViewDidEndEditing:self];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [textView resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextView: shouldChangeTextInRange: replacementText:)]) {
        return [self.textViewDelegate bdTextView:self shouldChangeTextInRange:range replacementText:text];
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    if (self.text.length != 0 || self.attributedText.length != 0) {
        self.placeHolderLabel.hidden = YES;
    }else {
        if (self.isFirstResponder == YES) {
            self.placeHolderLabel.hidden = YES;
        }else{
            self.placeHolderLabel.hidden = NO;
        }
    }
    
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextViewDidChange:)]) {
        [self.textViewDelegate bdTextViewDidChange:self];
    }
}

- (void)textViewDidChangeSelection:(UITextView *)textView{
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextViewDidChangeSelection:)]) {
        [self.textViewDelegate bdTextViewDidChangeSelection:self];
    }
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextView: shouldInteractWithURL:inRange:interaction:)]) {
        return [self.textViewDelegate bdTextView:self shouldInteractWithURL:URL inRange:characterRange interaction:interaction];
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(bdTextView: shouldInteractWithTextAttachment:inRange:interaction:)]) {
        return [self.textViewDelegate bdTextView:self shouldInteractWithTextAttachment:textAttachment inRange:characterRange interaction:interaction];
    }
    return YES;
}

@end
